package com.phone.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.phone.client.config.RsaKeyProperties;
import com.phone.client.mapper.ClientMapper;
import com.phone.client.poji.entry.TbClient;
import com.phone.client.poji.vo.ClientVo;
import com.phone.client.service.ClientService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-seventh
 * @description: ClientService的实现
 * @author: 杨佳琪
 * @create: 2021-07-23 11:35
 **/
@ApiModel(value="ClientService的实现", description="ClientService的实现")
@Service
public class ClientServiceImpl implements ClientService {
    @Resource
    private ClientMapper clientMapper;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;
    @ApiModelProperty("分页查询")
    @Override
    public R initClientTable(Integer currentPage, Integer pageSize, ClientVo clientVo) {
        Page<TbClient> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbClient> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(clientVo.getEmail())){
            wrapper.like("email",clientVo.getEmail());
        }
        if (StringUtils.isNotEmpty(clientVo.getPhoneNum())){
            wrapper.like("phone_num",clientVo.getPhoneNum());
        }
        if (StringUtils.isNotEmpty(clientVo.getPassword())){
            wrapper.eq("password",clientVo.getPassword());
        }
        if (StringUtils.isNotEmpty(clientVo.getUsername())){
            wrapper.like("username",clientVo.getUsername());
        }
        if (StringUtils.isNotEmpty(clientVo.getContactName())){
            wrapper.like("contact_name",clientVo.getContactName());
        }

        IPage<TbClient> page1=clientMapper.initClientTable(page,wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());



    }

    @ApiModelProperty("删除")
    @Override
    public R deleteClient(Integer id) {
        int i=clientMapper.deleteById(id);
        return R.ok();
    }

    @ApiModelProperty("修改")
    @Override
    public R updateClient(TbClient tbClient) {
        clientMapper.updateById(tbClient);
        return R.ok();
    }

    @ApiModelProperty("新增")
    @Override
    public R insertClient(TbClient tbClient) {
        clientMapper.insert(tbClient);
        return R.ok();
    }

    @ApiModelProperty("修改的回显")
    @Override
    public R findById(Integer id) {
       TbClient client =  clientMapper.selectById(id);
        return R.ok().put("client",client);
    }

    @ApiModelProperty("查询客户账号是否唯一")
    @Override
    public R selUsername(TbClient tbClient) {
        List<TbClient> list=clientMapper.selectList(null);
        int result=0;
        for (TbClient u:list) {
            if (tbClient.getId()!=null){
                if (!u.getId().equals(tbClient.getId())){
                    if (u.getUsername().equals(tbClient.getUsername())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getUsername().equals(tbClient.getUsername())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @ApiModelProperty("查询客户邮箱是否唯一")
    @Override
    public R selEmail(TbClient tbClient) {
        List<TbClient> list=clientMapper.selectList(null);

        int result=0;
        for (TbClient u:list) {
            if (tbClient.getId()!=null){
                if (!u.getId().equals(tbClient.getId())){
                   /* System.out.println(u.getEmail());
                    System.out.println(tbClient.getEmail());*/
                    if (u.getEmail().equals(tbClient.getEmail())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getEmail().equals(tbClient.getEmail())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @ApiModelProperty("查询客户电话是否唯一")
    @Override
    public R selPhone(TbClient tbClient) {
        List<TbClient> list=clientMapper.selectList(null);
        int result=0;
        for (TbClient u:list) {
            if (tbClient.getId()!=null){
                if (!u.getId().equals(tbClient.getId())){
                    if (u.getContactPhone().equals(tbClient.getContactPhone())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getContactPhone().equals(tbClient.getContactPhone())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @Override
    public R selCname(TbClient tbClient) {
        List<TbClient> list=clientMapper.selectList(null);
        int result=0;
        for (TbClient u:list) {
            if (tbClient.getId()!=null){
                if (!u.getId().equals(tbClient.getId())){
                    if (u.getContactName().equals(tbClient.getContactName())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getContactName().equals(tbClient.getContactName())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @Override
    public R upStatus(Integer id, Integer status) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        clientMapper.update(null,wrapper);
        return R.ok();
    }



}